package com.zahb.qadx.ui.activity.topic

import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import com.orhanobut.logger.Logger
import com.zahb.qadx.net.RetrofitService
import java.util.regex.Matcher
import java.util.regex.Pattern

class ImgStr {
    companion object {
        @JvmStatic
        fun getImgStr(html: String): String {
            var new_html = html
            var img = ""
            var p_image: Pattern
            var m_image: Matcher
            val regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>"
            p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE)
            m_image = p_image.matcher(html)
            while (m_image.find()) {
                img = m_image.group()
                val m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img)
                while (m.find()) {
//                获取到的img标签的src图片地址
                    val result = m.group(1)
                    Logger.d(result)


                    //然后根据需求替换为新的图片地址
                    when (result.contains("http")) {
                        true -> new_html = new_html.replace(result, result)
                        false -> new_html = new_html.replace(result, "http:" + result)
                    }

                }
            }



            //把新的富文本展示出来
            return new_html
        }


        @JvmStatic
        fun getWebview(webview: WebView ,context: Context,string: String) {
            val javaScriptInterface = MyJavaInterface(context)
            webview.addJavascriptInterface(javaScriptInterface, string)
            webview.webViewClient = object : WebViewClient(){
                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    setWebImageClick(view!!, string)

                }

                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                    view?.settings?.javaScriptEnabled = true
                    super.onPageStarted(view, url, favicon)
                }
            }

        }


        /**
         * 设置网页中图片的点击事件
         * @param view
         *
         */
        fun setWebImageClick(view: WebView, method: String) {
            var jsCode="javascript:(function(){" +
                    "var imgs=document.getElementsByTagName(\"img\");" +
                    "for(var i=0;i<imgs.length;i++){" +
                    "imgs[i].pos = i;"+
                    "imgs[i].οnclick=function(){" +
                    "window."+method+".openImage(this.src,this.pos);" +
                    "}}})()"
            Logger.d(jsCode)
            view.loadUrl(jsCode)


        }
        class MyJavaInterface(val context: Context) {

            @JavascriptInterface
            fun content(message: String) {

            }

            @JavascriptInterface
            fun pay(num: Int) {

            }
            @JavascriptInterface
            fun openImage(img: String, pos: Int){
//                DailyPracticeActivity.startFullImageActivity(context, img)/
                Log.e("ZOULE ","SASAS");
            }
        }



    }
}